﻿using System;
using System.Collections.Generic;
using System.Text;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Collections.Specialized;

namespace Duson.Controls
{
    [ToolboxData(@"<{0}:CompositeControl runat='server'>
    </{0}:CompositeControl>")]
    public class CompositeControl : WebControl//, IPostBackEventHandler, IPostBackDataHandler
    {
        private TextBox _txtName;
        private Button _btnOk;
        private Button _btnCancel;

        #region 属性
        [Browsable(true),
        Category("uer"),
        Description("rrrrrrr"),
        DefaultValue("444")
        ]
        public string Str
        {
            get { return ViewState["Str"] != null ? ViewState["Str"].ToString() : ""; }
            set { ViewState["Str"] = value; }
        }

        [Bindable(true),
        Category("app"),
        DefaultValue(""),
        Description("")]
        public string Name
        {
            get { EnsureChildControls();  return _txtName.Text; }
            set { EnsureChildControls(); _txtName.Text = value; }
        }

        [Bindable(true),
        Category("app"),
        DefaultValue("确定"),
        Description("")]
        public string BtnOkText
        {
            get { EnsureChildControls(); return _btnOk.Text; }
            set { EnsureChildControls(); _btnOk.Text = value; }
        }

        [Bindable(true),
        Category("app"),
        DefaultValue(""),
        Description("取消")]
        public string BtnCancelText
        {
            get { EnsureChildControls(); return _btnCancel.Text; }
            set { EnsureChildControls(); _btnCancel.Text = value; }
        }
        #endregion

        #region Click事件
        // 声明Click事件委托
        private static readonly object ClickKey1 = new object();
        private static readonly object ClickKey2 = new object();
        // 声明Click事件委托
        public event EventHandler Click1
        {
            add
            {
                Events.AddHandler(ClickKey1, value);
            }
            remove
            {
                Events.RemoveHandler(ClickKey1, value);
            }
        }

        public event EventHandler Click2
        {
            add
            {
                Events.AddHandler(ClickKey2, value);
            }
            remove
            {
                Events.RemoveHandler(ClickKey2, value);
            }
        }

        // 定义OnClick事件处理程序
        protected virtual void OnClick1(EventArgs e)
        {
            EventHandler clickEventDelegate = (EventHandler)Events[ClickKey1];
            if (clickEventDelegate != null)
            {
                clickEventDelegate(this, e);
            }
        }

        protected virtual void OnClick2(EventArgs e)
        {
            EventHandler clickEventDelegate = (EventHandler)Events[ClickKey2];
            if (clickEventDelegate != null)
            {
                clickEventDelegate(this, e);
            }
        }

        // 实现RaisePostBackEvent方法,处理回发事件
        //public void RaisePostBackEvent(string eventArgument)
        //{
        //    if (eventArgument == "a")
        //        OnClick1(new EventArgs());
        //    else if (eventArgument == "b")
        //        OnClick2(new EventArgs());
        //}

        protected override bool OnBubbleEvent(object source, EventArgs e)
        {
            bool handled = false;
            if (e is CommandEventArgs)
            {
                CommandEventArgs ce = (CommandEventArgs)e;
                if (ce.CommandName == "a")
                {
                    OnClick1(EventArgs.Empty);
                    handled = true;
                }
                else if (ce.CommandName == "b")
                {
                    OnClick2(new EventArgs());
                    handled = true;
                }
            }

            return handled;
        }

        #endregion

        #region TextChanged事件
        //public event EventHandler TextChanged;

        //public bool LoadPostData(string postDataKey, NameValueCollection postCollection)
        //{
        //    string postedValue = postCollection[postDataKey];
        //    //检查新旧数据
        //    if (!Str.Equals(postedValue))
        //    {
        //        Str = postedValue;
        //        return true;
        //        //自动调用RaisePostDataChangedEvent()
        //    }
        //    else
        //        return false;
        //    //不发生变化
        //}

        //public void RaisePostDataChangedEvent()
        //{
        //    OnTextChanged(new EventArgs());
        //}

        //protected virtual void OnTextChanged(EventArgs e)
        //{
        //    if (TextChanged != null)
        //        TextChanged(this, e);
        //}
        #endregion

        #region 输出内容
        protected override HtmlTextWriterTag TagKey
        {
            get { return HtmlTextWriterTag.Div; }
        }

        protected override void AddAttributesToRender(HtmlTextWriter writer)
        {
            //为table标签定义属性和样式
            writer.AddStyleAttribute(HtmlTextWriterStyle.BorderColor, "#ccc");
            writer.AddStyleAttribute(HtmlTextWriterStyle.BorderWidth, "1px");
            writer.AddStyleAttribute(HtmlTextWriterStyle.BorderStyle, "solid");
            writer.AddStyleAttribute(HtmlTextWriterStyle.Width, "400px");
            writer.AddStyleAttribute(HtmlTextWriterStyle.Height, "400px");

            base.AddAttributesToRender(writer);
        }

        protected override void CreateChildControls()
        {
            _txtName = new TextBox();
            _txtName.ID = _txtName.UniqueID;

            _btnOk = new Button();
            _btnOk.ID = _btnOk.UniqueID;
            _btnOk.CommandName = "a";

            _btnCancel = new Button();
            _btnCancel.ID = _btnCancel.UniqueID;
            _btnCancel.CommandName = "b";

            Controls.Add(_txtName);
            Controls.Add(_btnOk);
            Controls.Add(_btnCancel);
        }

        protected override void RenderContents(HtmlTextWriter writer)
        {
            _txtName.RenderControl(writer);
            //writer.AddAttribute(HtmlTextWriterAttribute.Onclick, Page.GetPostBackEventReference(this, "a"));
            _btnOk.RenderControl(writer);
            //writer.AddAttribute(HtmlTextWriterAttribute.Onclick, Page.GetPostBackEventReference(this, "b"));
            _btnCancel.RenderControl(writer);
        }
        #endregion
    }
}
